var util = require('../../utils/util.js')
var app = getApp();
var uid = 0;
var cpage = 1;
var hasMore = true;
Page({
  data: {
    org_name: '',
    tc: 0,
    hasData:false,
    footer_curr:["","curr"],
    userInfo: {},
    inviteCount: 0,//总邀请人数
    recents:[],
    imgDomain: app.globalData.imgDomain
  },
  goAtherPage: function(e){
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({url: url})
  },

  onLoad:function(options){
    var tc = app.get_tc();
    var titlename = tc == 1 ? '邀請記錄' : '邀请记录';
    wx.setNavigationBarTitle({
      title: titlename,
    })
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    var tc = app.get_tc();
    that.setData({
      tc: tc
    })
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });

    //获取绑定orgid信息
    app.GetUserInfo(util.orgid, function (res) {
      that.setData({
        org_name: res.uname
      })
    });

    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function(res){
      //更新数据
      that.setData({
        userInfo:res
      })
      uid = res.uid;
    }, inviteuid);
    hasMore = true;
    cpage = 1;
  },
  onReady:function(){
    // 页面渲染完成    
    var that = this;
    //获取邀请人数
    that.loadInviteCount();
    //请求当前用户的动态    
    that.loadInviteList(cpage);
  },

  //请求邀请总人数
  loadInviteCount: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "invitecount",
        "uid": uid
      },
      success: function (responseData) {
        console.log(responseData);
        that.setData({
          inviteCount: responseData.data.count ? responseData.data.count : 0
        });
      }
    });
  },

  //请求邀请列表数据
  loadInviteList: function(cpage){
    var that = this;
  util.api({
      data:{
        "publickey":util.publiccode,
        "ac":"inviteinfo",
        "uid":uid,
        "page":cpage
      },
      success:function(responseData){
        console.log(responseData);
        if (responseData.res == false) {
          that.setData({
            hasData: false,
            recents: []
          });
        } else {
          var data = responseData.data;
          if(data && data.length > 0) {
                that.setData({
                  hasData:true,
                    recents:that.data.recents.concat(data)
                });
          }
          
        }
      }
    });
  },

  //加载更多
  loadMore:function(){
      if(hasMore){
        cpage ++;
        this.loadInviteList(cpage);
      }
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  //页面分享
  onShareAppMessage: function () {
    var that = this;
    var invUid = uid ? uid : '';
    return {
      title: that.data.org_name,
      path: '/pages/league/league?inviteuid=' + invUid
    };
  },
  //保存图片
  saveImg: function(e) {
    var img = e.currentTarget.dataset.img;
    img = img.replace(/^http:/, 'https:');
    console.log(img);
    wx.downloadFile({
      url: img,
      type: 'image',
      success: function (res) {
        wx.saveFile({
          tempFilePath: res.tempFilePath,
          success: function (res) {
            var savedFilePath = res.savedFilePath
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
          },
          fail: function(e) {
            console.log(e)
          }
        })
      }
    })    
  },
  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },
})